import java.util.*;


public class Solution {
    LinkedHashMap<Integer, Integer> map;
    int cacapacity = 0;//长度
    int size = 0;
    public Solution(int capacity) {
        // write code here
        this.cacapacity = capacity;
        this.map = new LinkedHashMap<>(capacity);
    }

    public int get(int key) {
        // write code here
        Integer value = map.get(key);
        if(value == null){
            return -1;
        } else {
            map.remove(key);
            map.put(key, value);
            return value;
        }
    }

    public void set(int key, int value) {
        // write code here
        if(map.containsKey(key)){
            map.remove(key);
            map.put(key, value);
        } else {
            if(size < cacapacity){
                map.put(key ,value);
                size++;
            } else if(size == cacapacity){
                int first = map.keySet().iterator().next();
                map.remove(first);
                map.put(key, value);
            }
            
        }
        return;
    }
}

/**
 * Your Solution object will be instantiated and called as such:
 * Solution solution = new Solution(capacity);
 * int output = solution.get(key);
 * solution.set(key,value);
 */